<template>
  <view class="goods-detail-container" v-if="goods_info && goods_info.goods_name">
    <!-- 轮播图区域 -->
    <swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(detail, i) in goods_info.pics" :key="i"><image class="image" :src="detail.pics_big" @click="previewImg(i)"></image></swiper-item>
    </swiper>
    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <view class="price">¥{{ goods_info.goods_price }}</view>
      <view class="goods-body">
        <view class="goods-name">{{ goods_info.goods_name }}</view>
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yf">快递：免运费</view>
    </view>
    <!-- 商品详情信息 introduce -->
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>
    <!-- 商品导航组件 -->
    <view class="goods-nav">
      <!-- fill 控制右侧按钮的样式 -->
      <!-- options 左侧按钮的配置项 -->
      <!-- buttonGroup 右侧按钮的配置项 -->
      <!-- click 左侧按钮的点击事件处理函数 -->
      <!-- buttonClick 右侧按钮的点击事件处理函数 -->
      <uni-goods-nav fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick" @buttonClick="buttonClick" ></uni-goods-nav>
    </view>
  </view>
</template>

<script>
import {mapState, mapMutations, mapGetters} from 'vuex'
export default {
  data() {
    return {
      // 商品信息
      goods_info: {},
      // 底部按钮配置
      options: [
        {
          icon: 'shop',
          text: '店铺'
        },
        {
          icon: 'cart',
          text: '购物车',
          info: 2
        }
      ],
      buttonGroup:[
        {
          text: '加入购物车',
          backgroundColor: '#ff0000',
          color: '#fff'
        },
        {
          text: '立即购买',
          backgroundColor: '#ffa200',
          color: '#fff'
        }
      ]
    };
  },
  onLoad(options) {
    let goods_id = options.goods_id;
    // console.log(goods_id);
    this.getGoodsInfo(goods_id);
    // this.buttonClick()
  },
  computed:{
    ...mapState('cart', ['cart']),
    ...mapGetters('cart', ['total'])
  },
  watch:{
    total:{
      handler(newVal, oldVal){
        const findResult = this.options.find(opt => opt.text === '购物车')
        if(findResult){
          findResult.info = newVal
        }
      },
      // 如果没有加immediate这个属性 watc不会在页面初次加载完毕后立即侦听
      immediate: true
    }
  },
  methods: {
    // 获取商品详情
    async getGoodsInfo(goods_id) {
      const { data: res } = await uni.$http.get('/goods/detail', { goods_id });
      if (res.meta.status !== 200) {
        //请求失败时
        return uni.$showMsg();
      }
      // console.log(res.message);
      // 1、使用正则替换将goods_intruduce中的img标签设为 块级元素解决图片留白的问题
      // 2、使用正则替换将img路径后缀名替换为jpg 因为ios设备对webp的后缀的图片不怎么支持
      res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display: block;"').replace(/webp/g, 'jpg');
      this.goods_info = res.message;
    },
    // 预览图片
    previewImg(i) {
      uni.previewImage({
        current: i,
        urls: this.goods_info.pics.map(item => item.pics_big)
      });
    },
    // goods-nav左侧按钮 回调函数
    onClick(e){
      // console.log(e);
      if(e.content.text === "购物车"){
        // 跳转到购物车页面
        uni.switchTab({
          url: '/pages/cart/cart'
        })
      }
    },
    // goods-nav右侧按钮 回调函数
    buttonClick(e){
      // console.log(e);
      // 1、判断点击的是那个按钮 
      if(e.content.text === '加入购物车'){
        // 2、准备一个商品的信息对象
        let goods = {
          goods_id: this.goods_info.goods_id,
          goods_price: this.goods_info.goods_price,
          goods_count: 1,
          goods_name: this.goods_info.goods_name,
          goods_small_logo: this.goods_info.goods_small_logo,
          goods_state: true //商品的勾选状态
        }
        this.addToCart(goods)
      }
    },
    ...mapMutations('cart', ['addToCart'])
  }
};
</script>

<style lang="scss">
.goods-detail-container {
  background-color: #ffffff;
}
.swiper {
  height: 750rpx;
  .image {
    width: 100%;
    height: 100%;
  }
}
.goods-info-box {
  padding: 10px;
  padding-right: 0;
  .price {
    font-size: 18px;
    color: #c00000;
    margin: 10px 0;
  }
  .goods-body {
    display: flex;
    justify-content: space-between;
    .goods-name {
      padding-right: 10px;
      font-size: 13px;
    }
    .favi {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 120px;
      border-left: 1px solid #efefef;
      font-size: 12px;
      color: gray;
    }
  }
  .yf {
    margin: 10px 0;
    font-size: 12px;
    color: gray;
  }
}
.goods-detail-container{
  padding-bottom: 50px;
}
.goods-nav{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
}
</style>
